package com.ruoyi.ruoyiyinzheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ruoyiyinzheng.domain.bo.StTestBo;
import com.ruoyi.ruoyiyinzheng.domain.vo.StTestVo;
import com.ruoyi.ruoyiyinzheng.domain.StTest;
import com.ruoyi.ruoyiyinzheng.mapper.StTestMapper;
import com.ruoyi.ruoyiyinzheng.service.IStTestService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 测试格Service业务层处理
 *
 * @author cody
 * @date 2023-09-17
 */
@RequiredArgsConstructor
@Service
public class StTestServiceImpl implements IStTestService {

    private final StTestMapper baseMapper;

    /**
     * 查询测试格
     */
    @Override
    public StTestVo queryById(Long testId){
        return baseMapper.selectVoById(testId);
    }

    /**
     * 查询测试格列表
     */
    @Override
    public TableDataInfo<StTestVo> queryPageList(StTestBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StTest> lqw = buildQueryWrapper(bo);
        Page<StTestVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询测试格列表
     */
    @Override
    public List<StTestVo> queryList(StTestBo bo) {
        LambdaQueryWrapper<StTest> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StTest> buildQueryWrapper(StTestBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StTest> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTestName()), StTest::getTestName, bo.getTestName());
        return lqw;
    }

    /**
     * 新增测试格
     */
    @Override
    public Boolean insertByBo(StTestBo bo) {
        StTest add = BeanUtil.toBean(bo, StTest.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTestId(add.getTestId());
        }
        return flag;
    }

    /**
     * 修改测试格
     */
    @Override
    public Boolean updateByBo(StTestBo bo) {
        StTest update = BeanUtil.toBean(bo, StTest.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StTest entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除测试格
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
